package com.zlq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlq.reggie.common.R;
import com.zlq.reggie.dto.DishDto;
import com.zlq.reggie.dto.SetmealDto;
import com.zlq.reggie.entity.Category;
import com.zlq.reggie.entity.Setmeal;
import com.zlq.reggie.entity.SetmealDish;
import com.zlq.reggie.service.CategorySercice;
import com.zlq.reggie.service.SetmealDishService;
import com.zlq.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * The type Setmeal controller.
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategorySercice categorySercice;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * Save r.
     *
     * @param setmealDto the setmeal dto
     * @return the r
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("开始新增套餐........");
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     *
     * @param page     the page
     * @param pageSize the page size
     * @param name     the name
     * @return r
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("套餐分页查询......");
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        //查询条件，模糊查询
        queryWrapper.like(name != null,Setmeal::getName,name);
        //根据时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) ->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            //分类Id
            Long categoryId = item.getCategoryId();
            //根据分类ID查询分类对象
            Category category = categorySercice.getById(categoryId);
            if (category != null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     *
     * @param ids the ids
     * @return r
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除套餐");
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * Status r.
     *
     * @param status the status
     * @param ids    the ids
     * @return the r
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status,@RequestParam List<Long> ids){
        log.info("套餐起售停售状态修改....");
        setmealService.statusUpdate(status,ids);
        return R.success("起售状态修改成功");
    }

    /**
     * 根据id查询套餐信息和口味
     *
     * @param id the id
     * @return r
     */
    @GetMapping("/{id}")
    public R<SetmealDto> dishShow(@PathVariable Long id){
        log.info("根据id查询套餐信息....");
        SetmealDto byIdWithSetmeal = setmealService.getByIdWithSetmeal(id);
        return R.success(byIdWithSetmeal);
    }

    /**
     * 修改套餐数据进行保存
     *
     * @param setmealDto the setmeal dto
     * @return r
     */
    @PutMapping
    public R<String> edit(@RequestBody SetmealDto setmealDto){
        log.info("修改套餐数据.......");
        if (setmealDto == null){
            return R.error("请求异常");
        }
        if (setmealDto.getSetmealDishes() == null){
            return R.error("套餐没有添加菜品，请添加菜品");
        }
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        Long setmealDtoId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDtoId);
        //先进行删除
        setmealDishService.remove(queryWrapper);
        //再填充相关数据Setmeal_Dish
        for (SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(setmealDtoId);
        }
        //批量保存到setmeal_Dish
        setmealDishService.saveBatch(setmealDishes);
        setmealService.updateById(setmealDto);
        return R.success("套餐修改成功");
    }

    /**
     * List r.
     * 根据条件查询套餐数据
     * @param setmeal the setmeal
     * @return the r
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
